function PopulateAddresses() {

    //If google library not loaded then settime
    if (google == null) {
        google.load("maps", "3", { other_params: "libraries=places&sensor=false", "callback": PopulateAddresses });
        setTimeout(PopulateAddresses, 2000);
        return;
    }
    //If google library loaded but google maps libray not loaded then add those and settime to recall same function again
    if (google != null && google.maps == null) {
        google.load("maps", "3", { other_params: "libraries=places&sensor=false", "callback": PopulateAddresses });
        setTimeout(PopulateAddresses, 2000);
        return;
    }
    //if google map places is undefined then recall function after some time.. 
    if (google.maps.places == null) {
        setTimeout(PopulateAddresses, 2000);
        return;
    }

    //Get input control for Google places   
    var autocomplete;          
    var control = Xrm.Page.getAttribute('address1_composite').controls.get(0);
    if (control != null) {
        control.setFocus();
        //var input = $("#" + attributeName + "_i")
        var input = $("#address1_composite_compositionLinkControl_address1_line1_i");

        /*Sometimes google places list is not showing thats why need to add this style*/
        var pacContainerInitialized = false;
        input.keypress(function () {
            if (!pacContainerInitialized) {
                $('.pac-container').css('z-index', '9999');
                pacContainerInitialized = true;
            }
        });

        //add input to google autocomplete
        autocomplete = new google.maps.places.Autocomplete(document.getElementById('address1_composite_compositionLinkControl_address1_line1_i'));
    }

    
    /* Set up event listener for place selection */
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        /* Get place details */
        var place = autocomplete.getPlace();
        if (!place.geometry) {
            return;
        }
        /* Loop through the address components for the selected place and fill
         the corresponding input fields in CRM */

        var houseNumber, StreetName, city, state, Zip, Country;

        for (i = 0; i < place.address_components.length; i++) {

            var type = place.address_components[i].types[0];
            if (type == 'street_number') {
                houseNumber = place.address_components[i].long_name + "";
            }
            if (type == 'route') {
                StreetName = place.address_components[i].long_name;
            }
            if (type == 'locality' || type == 'administrative_area_level_3') {
                city = place.address_components[i].long_name;
            }
            if (type == 'postal_code') {
                Zip = place.address_components[i].long_name;
            }
            if (type == 'administrative_area_level_1') {
                state = place.address_components[i].short_name;
            }
            if (type == 'country') {
                Country = place.address_components[i].long_name;
            }
        }
            Xrm.Page.getAttribute('address1_line1').setValue(houseNumber);
            Xrm.Page.getAttribute('address1_line2').setValue(StreetName);
            Xrm.Page.getAttribute('address1_city').setValue(city);
            Xrm.Page.getAttribute('address1_postalcode').setValue(Zip);
            Xrm.Page.getAttribute('address1_stateorprovince').setValue(state);
            Xrm.Page.getAttribute('address1_country').setValue(Country);
    });

}